//
//  ViewController.m
//  06 View Switcher
//
//  Created by willter on 13-1-24.
//  Copyright (c) 2013年 willter. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize blueViewControll;
@synthesize yellowViewController;

- (void)viewDidLoad
{
    BlueViewController *blueController = [[BlueViewController alloc]  initWithNibName:@"BlueViewController" bundle:nil];
    self.blueViewControll = blueController;
    [self.view insertSubview:blueController.view atIndex:0];
    [blueController release];
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
}

- (IBAction)switchView:(id)sender {
    [UIView beginAnimations:@"View Flip" context:nil]; // 声明一个动画
    [UIView setAnimationDuration:1.25]; // 指定动画持续时间
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; // 设置动画曲线
    
    if (self.yellowViewController.view.superview == nil) {
        if (self.yellowViewController == nil) {
            YellowViewController *yellowController = [[YellowViewController alloc] initWithNibName:@"YellowViewController" bundle:nil];
            self.yellowViewController = yellowController;
            [yellowController release];
        }
        [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight
                               forView:self.view cache:YES];
        // 视图交换
        [self.blueViewControll viewWillAppear:YES];
        [self.yellowViewController viewWillDisappear:YES];
        [self.blueViewControll.view removeFromSuperview];
        [self.view insertSubview:self.yellowViewController.view atIndex:0];
        [self.yellowViewController viewDidDisappear:YES];
        [self.blueViewControll viewDidAppear:YES];
    } else {
        if (self.blueViewControll == nil) {
            BlueViewController *blueController = [[BlueViewController alloc]  initWithNibName:@"BlueViewController" bundle:nil];
            self.blueViewControll = blueController;
            [blueController release];
        }
        [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp
                               forView:self.view cache:YES];
        // 视图交换
        [self.yellowViewController viewWillAppear:YES];
        [self.blueViewControll viewWillDisappear:YES];
        [self.yellowViewController.view removeFromSuperview];
        [self.view insertSubview:self.blueViewControll.view atIndex:0];
        [self.blueViewControll viewDidDisappear:YES];
        [self.yellowViewController viewDidAppear:YES];
    }
    [UIView commitAnimations];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    if (self.blueViewControll.view.superview == nil) {
        self.blueViewControll = nil;
    } else {
        self.yellowViewController = nil;
    }
}

- (void) dealloc {
    [self.yellowViewController release];
    [self.blueViewControll release];
    [super dealloc];
}

@end
